﻿using AiShua.CookiePool.Task;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace AiShua.CookiePool
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            StartTask();
        }

        public void StartTask()
        {
            string jobkey = Guid.NewGuid().ToString();
            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            ITrigger trigger = TriggerBuilder.Create()
                                       .WithIdentity(string.Format("{0}-上下架触发器", jobkey), "触发器组")
                                       .StartNow()
                                       .WithCronSchedule("1 1 * * * ? ")    //秒     
                                       .Build();

            IJobDetail job = JobBuilder.Create<CheackAccount>()  //创建上下架
               .WithIdentity(string.Format("{0}", jobkey), "上下架作业组")
               .Build();

            scheduler.ScheduleJob(job, trigger);
            scheduler.Start();//创建新任务立即启动
        }
    }
}
